package one.stand.controller;

import com.github.pagehelper.PageHelper;
import com.jmp.api.admin.dto.CompanyEquAuditRequest;
import com.jmp.api.admin.dto.CompanyEquDeleteRequest;
import com.jmp.api.admin.dto.CompanyEquIndexRequest;
import com.jmp.api.admin.dto.CompanyEquIndexResponse;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import one.stand.dto.ConvertUtils;
import one.stand.dto.PageResponse;
import one.stand.mapper.CompanyApiAdminMapper;
import one.stand.model.Company;
import one.stand.model.CompanyEqu;
import one.stand.model.ResultModel;
import one.stand.service.CompanyBaseService;
import one.stand.service.CompanyEquService;
import one.stand.util.SqlUtil;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author cjr
 */
@Api(description = "设备号管理")
@RestController
@RequestMapping("adminApi/equ/")
public class EquController {
    @Autowired
    private CompanyEquService companyEquService;
    @Autowired
    private CompanyBaseService companyBaseService;
    @Autowired
    private CompanyApiAdminMapper companyApiAdminMapper;

    @ApiOperation(value = "设备号列表")
    @GetMapping("index")
    @SecurityAuthCheck
    public ResultModel<CompanyEquIndexResponse> index(CompanyEquIndexRequest request) {

        String nameLike = SqlUtil.like(request.getCompanyName());
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<CompanyEqu> equList = companyApiAdminMapper.selectEqus(request.getType(), nameLike);
        PageResponse<CompanyEquIndexResponse.Equ> equs = ConvertUtils.page(equList, companyEqu -> {
            CompanyEquIndexResponse.Equ equ = CompanyEquIndexResponse.Equ.builder()
                    .id(companyEqu.getId())
                    .equCode(companyEqu.getEquCode())
                    .status(companyEqu.getStatus())
                    .memo(companyEqu.getMemo())
                    .auditTime(Objects.nonNull(companyEqu.getAuditTime()) ? DateFormatUtils.ISO_8601_EXTENDED_DATETIME_FORMAT.format(companyEqu.getAuditTime()) : null)
                    .build();
            if (Objects.nonNull(companyEqu.getCompanyId())) {
                Company companyModel = companyBaseService.getById(companyEqu.getCompanyId());
                if (Objects.nonNull(companyModel)) {
                    equ.setCompanyId(companyModel.getCompanyId());
                    equ.setCompanyName(companyModel.getCompanyName());
                }
            }
            return equ;
        });

        CompanyEquIndexResponse response = CompanyEquIndexResponse.builder()
                .equs(equs)
                .build();
        return ResultModel.success(response);
    }

    @ApiOperation(value = "审核")
    @PostMapping("audit")
    @SecurityAuthCheck
    public ResultModel audit(@RequestBody CompanyEquAuditRequest request) {
        if (1 == request.getStatus()) {
            companyEquService.updatePass(request.getId(), request.getCompanyId(), request.getMemo());
        } else if (2 == request.getStatus()) {
            companyEquService.updateRefuse(request.getId(), request.getMemo());
        } else {
            companyEquService.updateMemo(request.getId(), request.getMemo());
        }
        return ResultModel.success();
    }

    @ApiOperation(value = "删除")
    @PostMapping("delete")
    @SecurityAuthCheck
    public ResultModel delete(@RequestBody CompanyEquDeleteRequest request) {
        companyEquService.delete(request.getId());
        return ResultModel.success();
    }
}
